package com.linkwedo.saas.project;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.linkwedo.saas.domain.Project;
import com.linkwedo.saas.domain.ProjectReview;
import com.linkwedo.saas.domain.Target;
import com.linkwedo.saas.domain.Task;
import com.linkwedo.saas.domain.member.User;
import com.linkwedo.saas.users.UserService;
import com.linkwedo.saas.util.DateUtil;
import com.linkwedo.saas.util.excel.ExcelUtil;
import com.opensymphony.xwork2.ActionSupport;
public class ExportOnePagePMAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 8190003683775267839L;
	Project project;
	List<Project> list;
	private ProjectService projectService;
	private UserService userService;
	private TargetService targetService;
	private TaskService taskService;
	private ProjectReviewService projectReviewService;
	
	private Integer id;
	private String projectId;
	private Timestamp reviewTime;
	private String path;
	private String note;
	
	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	public javax.servlet.http.HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(javax.servlet.http.HttpServletRequest request) {
		this.request = request;
	}

	private javax.servlet.http.HttpServletRequest request;
	private javax.servlet.http.HttpServletResponse response;

	

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public String execute() throws Exception {
		String projectId = request.getParameter("pid");
		if(null == projectId || "".equalsIgnoreCase(projectId)){
			return ERROR;
		}
		Integer projectIdValue = Integer.parseInt(projectId);
		project=getProjectService().getProjectById(projectIdValue);
		project.setTargetlist(getTargetService().findByProjectID(projectIdValue));
		project.setTasklist(getTaskService().findByProjectID(projectIdValue));
		project.setUserlist(getUserService().findAll());	
		
		String fileName = "one_page_project_template.xls";
		String realpath = request.getSession().getServletContext().getRealPath("/") ;//系统当前的绝对路径
		String currentDate = DateUtil.timestamp2Str(new Timestamp(new Date().getTime()));
		//模板名称
		if(null==fileName || "".equals(fileName))
			fileName = "one_page_project_template.xls";
		String path =realpath+ "templates\\" + fileName ;
		String newFile = realpath+"templates\\"+"one_page_project_"+projectId+".xls";//+"日期"+currentDate
		
		ProjectReview projectReview = new ProjectReview();
		projectReview.setProjectId(projectIdValue);
		projectReview.setReviewTime(DateUtil.str2Timestamp(currentDate));
		projectReview.setPath(newFile);
		projectReview.setProjectName(project.getName());
		projectReviewService.save(projectReview);
		
		ExcelUtil excelOp = new ExcelUtil();
		excelOp.generateNewFile(path, newFile, project);
		
		excelOp.respsonseExcel(response, newFile, "test.xls");
//		list=getProjectService().findAll();
//	    return "SUCCESS";
		return  null;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
		
	}



	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public TargetService getTargetService() {
		return targetService;
	}

	public void setTargetService(TargetService targetService) {
		this.targetService = targetService;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public javax.servlet.http.HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(javax.servlet.http.HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}

	public ProjectReviewService getProjectReviewService() {
		return projectReviewService;
	}

	public void setProjectReviewService(ProjectReviewService projectReviewService) {
		this.projectReviewService = projectReviewService;
	}
}
